import 'dart:io';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:nyh_motorcade_app/constant/constant.dart';
import 'package:nyh_motorcade_app/styles/colors.dart';
import 'package:nyh_motorcade_app/utils/system_utils.dart';
import 'package:photo_view/photo_view.dart';
import 'package:photo_view/photo_view_gallery.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'preview_photo_controller.dart';

class PreviewPhotoPage extends StatelessWidget {
  final controller = Get.put(PreviewPhotoController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colours.color_black,
      body: GestureDetector(
        onTap: () {
          Navigator.pop(context);
        },
        onLongPress: () {
          if (controller.isDownloadImage) {
            //showSaveImageDialog(context, controller.index]);
          }
        },
        child: Container(
          constraints: BoxConstraints.expand(
            height: SystemUtils.getScreenHeight(context),
            width: SystemUtils.getScreenWidth(context),
          ),
          child: Stack(
            alignment: Alignment.bottomCenter,
            children: [
              GetBuilder<PreviewPhotoController>(
                builder: (controller) {
                  return Positioned.fill(
                    child: PhotoViewGallery.builder(
                      scrollPhysics: const BouncingScrollPhysics(),
                      pageController: controller.pageController,
                      itemCount: controller.imageList.length,
                      gaplessPlayback: true,
                      loadingBuilder: (context, event) {
                        return Center(
                          child: Container(
                            width: 30.w,
                            height: 30.w,
                            child: CircularProgressIndicator(strokeWidth: 1),
                          ),
                        );
                      },
                      builder: (context, index) {
                        return PhotoViewGalleryPageOptions(
                          imageProvider: getImageProvider(controller.type, controller.imageList[index] ?? ""),
                          heroAttributes: PhotoViewHeroAttributes(tag: index),
                        );
                      },
                      onPageChanged: (index) {
                        controller.setIndex(index);
                      },
                    ),
                  );
                },
              ),
              GetBuilder<PreviewPhotoController>(
                builder: (controller) {
                  return Visibility(
                    visible: controller.isShowIndex,
                    child: Padding(
                      padding: EdgeInsets.only(bottom: 30.w),
                      child: Text(
                        "${controller.index + 1}/${controller.imageList.length}",
                        style: TextStyle(color: Colours.color_white, fontSize: 14.sp),
                      ),
                    ),
                  );
                },
              ),
            ],
          ),
        ),
      ),
    );
  }

  ImageProvider getImageProvider(String type, String path) {
    switch (type) {
      case Constant.PREVIEW_PHOTO_TYPE_FILE:
        return FileImage(File(path));
      case Constant.PREVIEW_PHOTO_TYPE_NETWORK:
        return NetworkImage(path);
      default:
        return NetworkImage(path);
    }
  }
}
